package StudyCode.summary.zipfile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class UnzipFilesToPath {
	public static void unzipFilesToPath(String jarPath,
	                                    String destinationDir) throws IOException {
		File file = new File(jarPath);
		JarFile jar = new JarFile(file);

		for (Enumeration<JarEntry> enums = jar.entries(); enums
				.hasMoreElements(); ) {
			JarEntry entry = (JarEntry) enums.nextElement();

			String fileName = destinationDir + File.separator
					+ entry.getName();
			File f = new File(fileName);

			if (fileName.endsWith("/")) {
				f.mkdirs();
			}

		}

		for (Enumeration<JarEntry> enums = jar.entries(); enums
				.hasMoreElements(); ) {
			JarEntry entry = (JarEntry) enums.nextElement();

			String fileName = destinationDir + File.separator
					+ entry.getName();
			File f = new File(fileName);

			if (!fileName.endsWith("/")) {
				InputStream is = jar.getInputStream(entry);
				FileOutputStream fos = new FileOutputStream(f);

				while (is.available() > 0) {
					fos.write(is.read());
				}

				fos.close();
				is.close();
			}
		}

		try {
			jar.close();
		} catch (Exception e) {

		}
	}
}
